package com.snail.snailvr.views.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.snail.snailvr.R;
import com.snail.snailvr.model.Video;
import com.snail.snailvr.utils.IntentUtils;
import com.snail.snailvr.utils.Util;
import com.snail.snailvr.widget.FlexibleDividerDecoration;
import com.snail.snailvr.widget.HorizontalDividerItemDecoration;
import com.snail.snailvr.widget.ThumbView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LocalVideoAdapter extends RecyclerView.Adapter<LocalVideoAdapter.ViewHolder>  implements
        FlexibleDividerDecoration.PaintProvider,
        FlexibleDividerDecoration.VisibilityProvider,
        HorizontalDividerItemDecoration.MarginProvider {

    private final List<Video> mVideoLists;
    private Context mContext;
    private Paint dividerPaint;
    private int dividerMarginLeft;
    public LocalVideoAdapter(Context context, List<Video> values) {
        mVideoLists = values;
        mContext=context;
        dividerPaint = Util.getDividerPaint();
        dividerMarginLeft = context.getResources().getDimensionPixelSize(R.dimen.input_divider_margin_left);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.local_video_item_layout, parent, false);
        return new ViewHolder(view);
    }

    private Video getItem(int position){
        if(mVideoLists!=null&&position<mVideoLists.size())
            return mVideoLists.get(position);
        return null;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final Video video=getItem(position);
        if(video!=null){
            holder.mVideoThum.setImageUrlAndReUse(video.getThum());
            holder.mVideoTitle.setText(video.getTitle());
            holder.mVideoDuration.setText(String.valueOf(video.getDuration()));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentUtils.playVideo(mContext,video.getPath(),video.getDisplayName());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mVideoLists.size();
    }

    @Override
    public int dividerLeftMargin(int position, RecyclerView parent) {
        return dividerMarginLeft;
    }

    @Override
    public int dividerRightMargin(int position, RecyclerView parent) {
        return 0;
    }

    @Override
    public Paint dividerPaint(int position, RecyclerView parent) {
        return dividerPaint;
    }

    @Override
    public boolean shouldHideDivider(int position, RecyclerView parent) {
        return false;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.video_thumb)
        ThumbView mVideoThum;
        @Bind(R.id.video_title)
        TextView mVideoTitle;
        @Bind(R.id.video_duration)
        TextView mVideoDuration;
        View itemView;
        public ViewHolder(View view) {
            super(view);
            itemView=view;
            ButterKnife.bind(this,view);
            mVideoThum.setImagePlaceHolder(R.drawable.ic_video_thumbnail_place_holder);
        }

    }
}
